﻿#include "VSingerNavigator.h"
#include "VPicture.h"
#include "VMain.h"
#include "VButton.h"
#include "SingerUi/CoverData.h"
#include "VSingerUi.h"

#include "VSingerCoverEffect.h"

#include "VDataModule.h"
#include "VText.h"

#include <boost/foreach.hpp>
#include <boost/smart_ptr.hpp>

#define foreach BOOST_FOREACH


extern SingerCoversDeque g_CoverDeque;

VSingerNavigator* VSingerNavigator::_pthis = NULL;
VPicture* VSingerNavigator::_defaultsinger = NULL;
VText* VSingerNavigator::_defaultname = NULL;

VSingerNavigator::VSingerNavigator(void) :
    _currenttab(0),
    _begin_ended(0),
    _end_ended(0)
{
    _pthis = this;
}

VSingerNavigator::~VSingerNavigator(void)
{
}

void VSingerNavigator::initButtons()
{
    int i;
    VButton* tab = NULL;

    for (i = 0; i != VSingerNavigator::max_count; ++i)
    {
        tab = new VButton;
        tab->loadTexture(VMain::getSkin("singer_nav\\%d.png", i + 1));
        tab->setVisible(false);
        tab->setGeometry(_x, _y, _w, _h);
        tab->setResponseGeometry(
            (default_res_x_start + (default_res_x_width + default_res_x_margin) * i),
            _y, default_res_x_width, _h);
        tab->setPrivateData(new int(i));
        tab->setClickHandler(&VSingerNavigator::switchToSpecialTab);

        insert(tab);
    }

    // All Actors
    switchToTab(7);
    //queryData(7);
}

void VSingerNavigator::switchToTab(int tabid)
{
    size_t i = 0;

    for (; i != _children->size(); ++i)
    {
        (*_children)[i]->setVisible(i == tabid);
    }

    _currenttab = tabid;
}

void VSingerNavigator::init()
{
    initDefaultAvatar();
    initDefaultName();
    initButtons();
}


bool VSingerNavigator::switchToSpecialTab(int, int, VWidget* triged)
{
    int id = *(int*)(triged->privateData());
    getThis()->switchToTab(id);//切换图片

    VSingerUi* ui = (VSingerUi*)VMain::queryUi(UI_SINGER);
    VSingerEffect* eff = ui->currentEffect();


    getThis()->_type = id;
    getThis()->loadData();


    return true;
}

VSingerNavigator* VSingerNavigator::getThis()
{
    return _pthis;
}

int VSingerNavigator::getCurrentTab()
{
    return _currenttab;
}

void VSingerNavigator::initDefaultAvatar()
{
    if (!_defaultsinger)
    {
        _defaultsinger = new VPicture(VMain::getSkin("defaultsinger.png"));
    }
}


VPicture* VSingerNavigator::getDefaultAvatar()
{
    return _defaultsinger;
}


void VSingerNavigator::loadData()
{
    if (!_pinyin.empty())
    {
        // TODO
    }

    VSingerUi* singerui = ((VSingerUi*)parent());
    ind_stype_sid_t* sind;
    int match_count;
    int i;

    match_count = findInd_stype_sid(_type, &sind);

    singerui->clear();

    for (i = 0; i != match_count; ++i)
    {
        singerui->push_back(sind[i].singer);
    }

    singerui->setupEffect();
}


VText* VSingerNavigator::getDefatultName()
{
    return _defaultname;
}

void VSingerNavigator::initDefaultName()
{
    if (!_defaultname)
    {
        _defaultname = new VText("斯琴格日乐");
    }
}

